package cn.itcast.travel.web.servlet;

import cn.itcast.travel.domain.ResultInfo;
import cn.itcast.travel.domain.User;
import cn.itcast.travel.domain.Weather;
import cn.itcast.travel.domain.vo.Comment;
import cn.itcast.travel.service.CommentService;
import cn.itcast.travel.service.WeatherService;
import cn.itcast.travel.service.impl.CommentServiceImpl;
import cn.itcast.travel.service.impl.WeatherServiceImpl;
import cn.itcast.travel.util.JedisUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/comment/*")
public class CommentServlet extends BaseServlet{

    private ObjectMapper om = new ObjectMapper();
    private CommentService sc = new CommentServiceImpl();
    private WeatherService ws = new WeatherServiceImpl();
    /**
     * 用户评论线路
     * @param request
     * @param response
     */
    public void sendComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        //获取线路id
        String rid = request.getParameter("rid");
        //获取用户评论的内容
        String content = request.getParameter("content");
        //获取登录用户
        User redisObject = (User) JedisUtil.getRedisObject(request.getSession().getId());
        if(redisObject==null){
            //用户没有登录
            ResultInfo r = new ResultInfo();
            r.setFlag(false);
            r.setErrorMsg("您还没有登录");
            String string = om.writeValueAsString(r);
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write(string);
            return;
        }

        //调用service的方法处理评论的逻辑
        sc.addCommit(rid,redisObject.getUid(),content);
        ResultInfo r = new ResultInfo();
        r.setFlag(true);
        r.setErrorMsg("评论成功！！");
        String string = om.writeValueAsString(r);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(string);


    }

    /**
     * 发送评论数据到前台展示
     * @param request
     * @param response
     * @throws IOException
     */
    public void showComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取线路id
        String rid = request.getParameter("rid");
        //调用service得到vo对象
        List<Comment> commit = sc.getCommit(rid);
        String string = om.writeValueAsString(commit);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(string);
    }

    /**
     * 获取天气情况
     * @param request
     * @param response
     */
    public void getWeather(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取需要查询的城市
        String city = request.getParameter("city");
        //调用service层获得天气对象
        Weather weather = ws.getWeatherByCite(city);
        String string = om.writeValueAsString(weather);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(string);
    }

}
